<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Util\SqlServerUtil;
use App\Util\Common;
use App\Services\OSS;
use App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\QueryException;
define('IS_OPEN_LOG', true);//false true

class TestController extends CommonController
{
    protected $response = [
		"code" => 200,
		"message" => "接口请求成功",
		"result" => []
	];
	protected $SqlServer = null;

	public function __construct(Request $request){
		//dd(DB::table('t_rp_rbillentry')->count() );
		$temp = SqlServerUtil::getModel($request);
		if($temp){
			$this->SqlServer = $temp['SqlServer'];
		}
	}

	public function abc(Request $request){
		$postData=[
			    "ApplyUser"=>"深圳市精锐通实业有限公司",
				"Contact"=> "韩翠平",
				"Remark"=>"",
				"Mobile"=> "13714388820",
				"Details"=> [
					"MaterialCode"=> "CC1206KKX7RCBB103",
					"Quantity"=> 540000,
					"Unit"=> "PCS"
					]
		];
		
		
		$str = json_encode($postData);
		$sign = md5($str.'0f2800b8ae164dedb99be3da416f6cf0');
		$res = $this->curlPost("https://yuncang.hqbuy.com/gys/OpenApi/Supplier/ApplyOutStock?signature=".$sign,$str);
		$this->log(__FUNCTION__,'----1----'.$res);
		
		
		dd($res);
		
		
		dd(222222);
		$givenSign = env('customerName');
		return response()->json($givenSign);
	}
	public function receipt(Request $request){
		$type = $request->get('type');
		$tData = [
                'title' => 'test',
                'remark' => 'remark',              
            ];
		
		$ret = DB::table('test')->insert($tData);
		//return response()->json($ret);
		//$ret = SqlServerUtil::exec($this->SqlServer, "Insert Into test(title,remark) values ('test".time()."','remark".time()."')", 2);
		//$ret = SqlServerUtil::exec($this->SqlServer, "select * from test", 2);
		$ret = DB::table('test')->count();
		return response()->json($ret);
	}
	
	//模拟POST提交
	private function curlPost($url, $data,$showError=1){
		$ch = curl_init();
		$header = ['auth:'.'f844b72e956247868f6be6a4de444e58','content-type:application/json'];
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		$tmpInfo = curl_exec($ch);
		if (curl_errno($ch))
		{
			echo 'Errno' . curl_error($ch); //捕抓异常
		}
		curl_close($ch); // 关闭CURL会话
		return $tmpInfo; // 返回数据
	}

}
